To fully implement this code you have to understand the Apple's Model View Controller concept and know how to use the Interface Builder.

You may use this sourcecode as you need. Please, don't email with the questions regarding the code. Use Apple’s documentation, books and Google instead. This source code comes with absolutely no warranty!

 

(c) Radif Sharafullin 2008

-------------------------------------------------- 

MainView.h

#import <UIKit/UIKit.h>

#import <Foundation/Foundation.h>


@interface MainView :UIView {

    IBOutlet UIView *aboutView;

IBOutlet UIView *sourceView;

    IBOutlet UILabel *daysLabel;

    IBOutlet UIView *pickerView;

IBOutlet UILabel *birthdayLabel;

IBOutlet UILabel *birthdayDays;

IBOutlet UIDatePicker *datePicker;

IBOutlet UIButton *celebsButton;

IBOutlet UILabel *aforismLabel;

IBOutlet UIWebView *webView;

float timeint;

bool BirthdayToday;

NSNumber *amountOfDays;

NSNumber *daysTillBirthday;

NSString *amountOfDaysString;

NSString *fileNM;

NSString *recordingDirectory;

NSCalendar *calendar;

NSDateComponents *dateComponents;

NSDateComponents *nowdateComponents;

NSDate *newDate;

NSDate *readDate;

NSString *linkString;

NSString *linkString1;

}




- (IBAction)aboutDonePressed;

- (IBAction)emailPressed;

- (IBAction)infoPressed;

- (IBAction)pickerDonePressed;

- (IBAction)pickerValueChanged;

- (IBAction)setBirthDayPressed;

- (IBAction)celebsPressed;

- (IBAction)celebsTodayPressed;

- (IBAction)appstorePressed;

- (IBAction)sourcedonePressed;

- (IBAction)sourceshowPressed;

- (IBAction)serverStartPressed;

-(void)implementInterface;




@property (nonatomic, retain) NSString *recordingDirectory;

@property (nonatomic, retain) NSString *linkString;

@property (nonatomic, retain) NSString *linkString1;


@end

 

 

--------------------------------------------------

MainView.m

#import "MainView.h"

#import "WebServer.h"


@implementation MainView


@synthesize recordingDirectory,linkString,linkString1;


-(void)awakeFromNib{

calendar = [NSCalendar currentCalendar];

timeint= 0.000000;

newDate = [NSDate date];

[datePicker setMaximumDate:newDate];

//Working with Date Components

dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:newDate];

 

[dateComponents setYear:1901];

[dateComponents setMonth:1];

[dateComponents setDay:1];

[dateComponents setHour:0];

[dateComponents setMinute:0];

 

// Assigning the date to a variable

newDate = [calendar dateFromComponents:dateComponents];

//Configuring DatePicker

[datePicker setMinimumDate:newDate];

[datePicker setMultipleTouchEnabled:YES];

 

//Implement if birthday is not saved

NSArray *filePaths = NSSearchPathForDirectoriesInDomains (

 

NSDocumentDirectory, 

NSUserDomainMask,

YES

); 

self.recordingDirectory = [filePaths objectAtIndex: 0];

fileNM=[NSString stringWithFormat: @"%@/Birthday.date",self.recordingDirectory];

CFURLCreateWithFileSystemPath (

  NULL,

  fileNM,

  kCFURLPOSIXPathStyle,

  false

  );

if ([[NSFileManager defaultManager] fileExistsAtPath:fileNM]) {

readDate =[NSKeyedUnarchiver unarchiveObjectWithFile:fileNM];

[datePicker setDate:readDate];

//Implement Interface

[self implementInterface];

}

else

{

[dateComponents setYear:1970];

[dateComponents setMonth:1];

[dateComponents setDay:1];

[dateComponents setHour:0];

[dateComponents setMinute:0];

newDate = [calendar dateFromComponents:dateComponents];

[datePicker setDate:newDate];

[self addSubview:pickerView];

}

//Opening the "source.html" in the WebView (source.html in the file you are looking at)

NSBundle *bundle = [NSBundle mainBundle]; 

NSString *path = [bundle bundlePath];

NSString *fullPath = [NSBundle pathForResource:@"source" ofType:@"html" inDirectory:path];

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fullPath]]];

}


// This function calculates the days and assigns the text on the screen labels

-(void)implementInterface{


int NEWValue;

//Calculating time difference (Value in seconds)

timeint = [[NSDate date] timeIntervalSinceDate: readDate];


//Convert to minutes

timeint=timeint/60;

//Convert To Hours

timeint=timeint/60;

//Convert to days

timeint=timeint/24;

//Convert To Integer Value

amountOfDays=[NSNumber numberWithInt:timeint];

// Placing the <comma> to separate thousands and assigning text on the daysLabel

int I,I1;

if ([amountOfDays intValue] > 999){

I = [amountOfDays intValue] /1000;

I1=[amountOfDays intValue] -I*1000;

if (I1<100){

amountOfDaysString=[NSString stringWithFormat:@"%i,0%i",I,I1];

if (I1<10){

amountOfDaysString=[NSString stringWithFormat:@"%i,00%i",I,I1];

}

}

else{

amountOfDaysString=[NSString stringWithFormat:@"%i,%i",I,I1];

}

}

else{

amountOfDaysString=[NSString stringWithFormat:@"%i",[amountOfDays intValue]];

}

[daysLabel setText:amountOfDaysString];

[birthdayLabel setText:[NSString stringWithFormat:@"You were born on: %@",[readDate dateWithCalendarFormat:@"%a, %b %e, %Y" timeZone:[NSTimeZone localTimeZone]]]];

//Implement birthday

calendar = [NSCalendar currentCalendar];

dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:readDate];

nowdateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit)fromDate:[NSDate date]];

[dateComponents setYear:[nowdateComponents year]];

///Getting the amount of days till the next birthday

//If the month is past

BirthdayToday=NO;

if([dateComponents month]<[nowdateComponents month]){

[dateComponents setYear:[nowdateComponents year]+1];

}

else

{

if([dateComponents month]==[nowdateComponents month])

{

if([dateComponents day]<[nowdateComponents day])

{

[dateComponents setYear:[nowdateComponents year]+1];

}

else

{

if([dateComponents day]==[nowdateComponents day]) BirthdayToday=YES;

}

}

}

newDate = [calendar dateFromComponents:dateComponents];

//Count the birthday days (timeint is in seconds)

timeint = [newDate timeIntervalSinceDate: [NSDate date]];

timeint=timeint/60;

//Convert To Hours

timeint=timeint/60;

//Convert to days

timeint=timeint/24;

daysTillBirthday=[NSNumber numberWithInt:timeint];


NEWValue=[dateComponents year];//Year of the next birthday

dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:readDate];

NEWValue=NEWValue-[dateComponents year];

//Custom Labels

if (BirthdayToday)

{

birthdayDays.textColor=[UIColor yellowColor];

birthdayDays.text= [NSString stringWithFormat:@"Happy Birthday!!! Your are %i today!",NEWValue];

}

else

{

if ([daysTillBirthday intValue]>0){

birthdayDays.textColor=[UIColor whiteColor];

birthdayDays.text= [NSString stringWithFormat:@"%i days left till you are %i years old",[daysTillBirthday intValue]+1 ,NEWValue];

}

else

{

birthdayDays.textColor=[UIColor yellowColor];

birthdayDays.text= [NSString stringWithFormat:@"You will be %i tomorrow!!!",NEWValue];

}

}

//Adding the button caption "Great people born on <your birthday>

linkString =[NSString stringWithFormat:@"Great people born on %@",[readDate dateWithCalendarFormat:@"%B %e" timeZone:[NSTimeZone localTimeZone]]];

[celebsButton setTitle:linkString forState:1];

[celebsButton setTitle:linkString forState:0];

////Phrases

//Amount of expressions

int amoutOfPhareses, phraseNumber;

amoutOfPhareses=44;

//Generating the random number from 1 to 44

phraseNumber=arc4random()%amoutOfPhareses+1;

//Selecting the expression based on the generated number

switch (phraseNumber) {

case 1:

[aforismLabel setText:@"Another day another dollar"];

break;

case 2:

[aforismLabel setText:@"If I would would get a nickel for every day I wore these shoes..."];

break;

case 3:

[aforismLabel setText:@"I wonder how many of them were rainy?"];

break;

case 4:

[aforismLabel setText:@"How many of them were you married"];

break;

case 5:

[aforismLabel setText:@"Haven't had the best one yet though ))"];

break;

case 6:

[aforismLabel setText:@"The secret of staying young is to live honestly, eat slowly, and lie about your age. - Lucille ball"];

break;

case 7:

[aforismLabel setText:@"The older the fiddler, the sweeter the tune. English Proverb"];

break;

case 8:

[aforismLabel setText:@"Age is strictly a case of mind over matter. If you don't mind, it doesn't matter. - Jack Benny"];

break;

case 9:

[aforismLabel setText:@"The old believe everything; the middle-aged suspect everything; the young know everything. - Oscar Wilde"];

break;

case 10:

[aforismLabel setText:@"Whatever with the past has gone, the best is always yet to come. - Lucy Larcom"];

break;

case 11:

[aforismLabel setText:@"It takes a long time to grow young. - Pablo Picasso"];

break;

case 12:

[aforismLabel setText:@"It is better to wear out than to rust out. - Bishop Richard Cumberland"];

break;

case 13:

[aforismLabel setText:@"Sugar in the gourd and honey in the horn, I never was so happy since the hour I was born"];

break;

case 14:

[aforismLabel setText:@"Very early, I knew that the only object in life was to grow. - Margaret Fuller"];

break;

case 15:

[aforismLabel setText:@"Youth is happy because it has the ability to see beauty. Anyone who keeps the ability to see beauty never grows old. - Franz Kafka"];

break;

case 16:

[aforismLabel setText:@"The secret to eternal youth is arrested development. - Alice Roosevelt Longworth"];

break;

case 17:

[aforismLabel setText:@"There is no cure for birth and death save to enjoy the interval. - George Santayana"];

break;

case 18:

[aforismLabel setText:@"Youth comes but once in a lifetime. - Henry Wadsworth Longfellow"];

break;

case 19:

[aforismLabel setText:@"Keep true to the dream of thy youth. - Friedrich Von Schiller"];

break;

case 20:

[aforismLabel setText:@"Everything I know I learned after I was thirty. - Georges Clemenceau"];

break;

case 21:

[aforismLabel setText:@"Nature gives you the face you have at twenty, but it's up to you to merit the face you have at fifty. - Coco Chanel"];

break;

case 22:

[aforismLabel setText:@"Grow old along with me! The best is yet to be,... - Robert Browning"];

break;

case 23:

[aforismLabel setText:@"After all, tomorrow is another day. - Margaret Mitchell"];

break;

case 24:

[aforismLabel setText:@"All my possessions for one moment of time. - Queen Elizabeth!"];

break;

case 25:

[aforismLabel setText:@"One to-day is worth two tomorrows. - Benjamin Franklin"];

break;

case 26:

[aforismLabel setText:@"The more things change, the more they remain the same. - Alphonse Karr"];

break;

case 27:

[aforismLabel setText:@"Every ten years a man should give himself a good kick in the pants. - Edward Steichen"];

break;

case 28:

[aforismLabel setText:@"If only I may grow: firmer, simpler-quieter, warmer. - Dag Hammarskjold"];

break;

case 29:

[aforismLabel setText:@"We are always the same age inside. - Gertrude Stein"];

break;

case 30:

[aforismLabel setText:@"I am long on ideas, but short on time. I expect to live to be only about a hundred. - Thomas Alva Edison"];

break;

case 31:

[aforismLabel setText:@"Time draweth wrinkles in a fair face, but addeth fresh colors to a fast friend. - John Lyle"];

break;

case 32:

[aforismLabel setText:@"Friendship is the shadow of the evening, which strengthens with the setting sun of life. - Jean De La Fontaine"];

break;

case 33:

[aforismLabel setText:@"I love everything that's old: old friends, old times, old manners, old books, old wines. - Oliver Goldsmith"];

break;

case 35:

[aforismLabel setText:@"Try to keep your soul young and quivering right up to old age. - George Sand"];

break;

case 36:

[aforismLabel setText:@"There are days of oldness, and then one gets young again. - Katherine Butler Hathaway"];

break;

case 37:

[aforismLabel setText:@"Live as long as you may, the first twenty years are the longest half of your life. - Robert Southey"];

break;

case 38:

[aforismLabel setText:@"The first hundred years are the hardest. - Wilson Mizner"];

break;

case 39:

[aforismLabel setText:@"There must be a day or two in a man's life when he is the precise age for something important. - Franklin P. Adams"];

break;

case 40:

[aforismLabel setText:@"Time flies like an arrow. Fruit flies like a banana. - Groucho Marx"];

break;

case 41:

[aforismLabel setText:@"Today is the yesterday you worried about tomorrow"];

break;

case 43:

[aforismLabel setText:@"You're not getting older, you're getting better."];

break;

case 44:

[aforismLabel setText:@"Few women admit their age. Few men act theirs. - Anon"];

break;

}


}


-(void)setBirthdayLabel

{

//this function is empty in this version

}


- (IBAction)aboutDonePressed {

// Transition view from About View to Main View

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:.7];

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES];

[aboutView removeFromSuperview];

[UIView commitAnimations];

}


- (IBAction)emailPressed {

//Initiate an email application with the address itoursoftware@gmail.com , Subject: "About iDays"; Use "%20" instead of " "

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:itoursoftware@gmail.com?subject=About%20iDays%20Sourcecode"]];

}

- (IBAction)appstorePressed {

// Launching the appstore

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=301351430&mt=8"]];

}

- (IBAction)infoPressed {

UIAlertView *baseAlert = [[UIAlertView alloc]

  initWithTitle:@"Interface Builder" message:@"The following function is called\nOn the 'Touch Up Inside' event\n\n- (IBAction)infoPressed\n\nSee the source code for details"

  delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil];

//[baseAlert fon

[baseAlert show];

[baseAlert release];

//Transition views from Main View to About View

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:.7];

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self cache:YES];

[self addSubview:aboutView];

[UIView commitAnimations];

}


- (IBAction)pickerDonePressed {

UIAlertView *baseAlert = [[UIAlertView alloc]

  initWithTitle:@"Interface Builder" message:@"The following functions are called\nOn the 'Touch Up Inside' event\n\n- (IBAction)pickerDonePressed\n\[self implementInterface]\n\nSee the source code for details"

  delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil];

[baseAlert show];

[baseAlert release];

//Transition views from Birthday Picker View to Main View

    [UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:.7];

[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self cache:YES];

[pickerView removeFromSuperview];

[UIView commitAnimations];

readDate=[datePicker date];

//Write to file

[NSKeyedArchiver archiveRootObject:readDate toFile:fileNM];

[self implementInterface];

}


- (IBAction)pickerValueChanged {

//Not implemented in this version

    

}



- (IBAction)celebsPressed

{

// Launching the Safari with the address containing the birthday "http://www.celebrity-link.com/birthday_day-%i_month-%i.html"

calendar = [NSCalendar currentCalendar];

dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:readDate];

linkString =[NSString stringWithFormat:@"http://www.celebrity-link.com/birthday_day-%i_month-%i.html",[dateComponents day],[dateComponents month]];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:linkString]];


//

}

//This IBAction opens the webpage with your birthday (sorry for the confusing name:setYourGoalsPressed)

- (IBAction)setYourGoalsPressed{



[[UIApplication sharedApplication] openURL:[NSURL URLWithString:linkString1]];

}


- (IBAction)celebsTodayPressed{

calendar = [NSCalendar currentCalendar];

dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]];

linkString =[NSString stringWithFormat:@"http://www.celebrity-link.com/birthday_day-%i_month-%i.html",[dateComponents day],[dateComponents month]];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:linkString]];

}


// Transitions and animations

- (IBAction)setBirthDayPressed {

UIAlertView *baseAlert = [[UIAlertView alloc]

  initWithTitle:@"Interface Builder" message:@"The following function is called\nOn the 'Touch Up Inside' event\n\n- (IBAction)setBirthDayPressed\n\nSee the source code for details"

  delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil];

[baseAlert show];

[baseAlert release];

    [UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:.7];

[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self cache:YES];

[self addSubview:pickerView];

[UIView commitAnimations];

}


- (IBAction)sourcedonePressed{

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:.7];

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES];

[sourceView removeFromSuperview];

[UIView commitAnimations];

}

- (IBAction)sourceshowPressed{

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:.7];

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self cache:YES];

[self addSubview:sourceView];

[UIView commitAnimations];

}


-(IBAction)serverStartPressed{

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[WebShareController sharedInstance]];


//[sourceView removeFromSuperview];

nav.view.frame =[[UIScreen mainScreen] bounds];

[sourceView addSubview:nav.view];


//[nav release];

}



- (void) dealloc

{

[amountOfDaysString dealloc];

[amountOfDays dealloc];

[super dealloc];

}



@end